package com.dhj.jvmstudy.chapter08;

/**
 * 引用计数算法测试，证明：Java使用的不是引用计数算法
 * -XX:+PrintGCDetails
 */
public class RefCountGC {
    // 这个成员属性的唯一作用就是占用一点内存
    private byte[] bigSize = new byte[5*1024*1024]; // 5MB

    // 引用
    Object reference = null;

    public static void main(String[] args) {
        RefCountGC obj1 = new RefCountGC();
        RefCountGC obj2 = new RefCountGC();

        obj1.reference = obj2;
        obj2.reference = obj1;

        obj1 = null;
        obj2 = null;

        // 显式的执行垃圾收集行为
        // 这里发生GC，obj1和obj2是否被回收？
        //System.gc();
    }
}
// 运行结果
/**
 * 不显式执行GC
 * Heap
 *  PSYoungGen      total 75264K, used 15401K [0x000000076c500000, 0x0000000771900000, 0x00000007c0000000)
 *   eden space 64512K, 23% used [0x000000076c500000,0x000000076d40a640,0x0000000770400000)
 *
 * 显式执行GC
 * [GC (System.gc()) [PSYoungGen: 14110K->1240K(75264K)] 14110K->1248K(247296K), 0.0019837 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [Full GC (System.gc()) [PSYoungGen: 1240K->0K(75264K)] [ParOldGen: 8K->999K(172032K)] 1248K->999K(247296K), [Metaspace: 3476K->3476K(1056768K)], 0.0058055 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
 * Heap
 *  PSYoungGen      total 75264K, used 1935K [0x000000076c500000, 0x0000000771900000, 0x00000007c0000000)
 *   eden space 64512K, 3% used [0x000000076c500000,0x000000076c6e3ef8,0x0000000770400000)
 */
